$(function () {
    $('#btn-admin').click(function () {
        let result = confirm("是否进入管理员登录?");
        if (result === true) {
            $(location).attr('href', '/adminLogin.html');
        } else {

        }
    })
    $('#btn-student').click(function () {
        let result = confirm("是否重新登录?");
        if (result === true) {
            $(location).attr('href', '/studentLogin.html');
        } else {

        }

    })
    $('#btn-dormInfo').click(function () {
        $(location).attr('href', '/student/getStudentDormInfo/' + sno);
    })
    $('#btn-applyDorm').click(function () {
        $(location).attr('href', '/admin/getDormApplication');
    })
    $('#btn-stuDorm').click(function () {
        $(location).attr('href', '/admin/getStudentDorm/1/5');
    })
    $('#btn-quitDorm').click(function () {
        $(location).attr('href', '/admin/getQuitStuDorm');
    })
    $('#btn-update-Dorm-Application').click(function () {
        $(location).attr('href', '/admin/getUpdateStuDorm');
    })
//安排课程
    /* $('#arrange').click(function () {
         console.log(lesson_id);
         alert(lesson_id);
         let judge = confirm("确定安排本节课的课程?");
         if (judge === true) {
             $(location).attr('href', '/admin/arrangeCourse/' + classId + '/' + day_id + '/' + lesson_id);
         }
     })*/
//排课
    /*    $('#btn-confirm').click(function (){
            /!* let data = $('#btn-confirm').val();*!/
            // if (data!==null){
            let judge = confirm("确定安排本节课的课程?");
            if (judge===true){
                if (number>3){
                    alert("今天已经排了很多这门课呢!请让老师休息一下");
                    $(location).attr('href', '/admin/arrangeCourse/' + classId + '/' + day_id + '/' + lesson_id);
                }
                else {
                    $.get({
                        url:"/admin/ajax/arrangeCourse",
                        data:{"classId":classId,"day_id":day_id,"lesson_id":lesson_id,"subject":subject},
                        //async: false, method: 'get',
                        success:function (result){
                            if (result===true){
                                alert("排课成功!");
                                $(location).attr('href', '/admin/courseArrange/' +classId+'/'+day_id);
                            }
                        }

                    })
                }

            }
            // }
        })*/
//申请换寝
    $('#btn-dormChange').click(function () {
        $.get({
            url: "/student/ajax/checkSign",
            data: {"sno": sno},
            success: function (result) {
                if (result === true) {
                    alert("你已经操作过了了!请勿在修改");
                    $('#btn-dormChange').hide();
                } else {
                    $(location).attr('href', '/student/toUpdateStuDorm/' + sno);
                }
            }
        })
    })
// 住校学生
    $('#btn-dorm').click(function () {
        $(location).attr("href", "/admin/getDormApplication");
    })
    $('#btn-selfInfo').click(function () {
        $(location).attr("href", "/homePage.html");
    })
// 同意换寝
    $('#btn-agree2').click(function () {
        let sno = $('#sno').text();
        let judge = confirm("是否同意此次换寝?");
        if (judge === true) {
            $.get({
                url: "/admin/ajax/agreeUpdateDorm",
                data: {"sno": sno},
                async: false, method: 'get',
                success: function (result) {
                    if (result === true) {
                        alert("更改成功!");
                        $(location).attr('href', '/admin/getUpdateStuDorm');
                    }
                }
            })
        } else {

        }

    })
// 拒绝换寝
    $('#btn-refuse2').click(function () {

        let sno = $('#sno').text();
        let judge = confirm("是否同意此次换寝?");
        if (judge === true) {
            $.get({
                url: "/admin/ajax/refuseUpdateDorm",
                data: {"sno": sno},
                async: false, method: 'get',
                success: function (result) {
                    if (result === true) {
                        alert("更改成功!");
                        $(location).attr('href', '/admin/getUpdateStuDorm');
                    }
                }
            })
        } else {

        }
    })
//申请走读
    $('#btn-free').click(function () {
        let judge = confirm("是否申请走读?");
        if (judge === true) {
            let reason = prompt("申请理由");
            $.get({
                url: "/student/ajax/judgeStuDormSign",
                data: {"sno": sno},
                success: function (result) {
                    if (result === true) {
                        $.get({
                            url: "/student/ajax/quitDorm",
                            data: {"sno": sno, "sign": 1},
                            success: function (result) {
                                if (result === true) {
                                    $.get({
                                        url: "/student/ajax/applyReason",
                                        data: {"sno": sno, "reason": reason},
                                        success: function (result) {
                                            if (result === true) {
                                                alert("申请成功!");
                                            } else {
                                                $.get({
                                                    url: "/student/ajax/quitDorm",
                                                    data: {"sno": sno, "sign": 0},
                                                    success: function (result) {
                                                        alert("上传失败!");
                                                    }
                                                })
                                            }
                                        }
                                    })
                                    console.log("申请成功");
                                } else {
                                    alert("申请失败!");
                                }
                            }
                        })
                    } else {
                        alert("你的申请正在审核!");
                    }
                }
            })
        } else {

        }
    })
    $('#btn-updateDorm').click(function () {
        //debugger
        let judge = confirm("是否申请换寝?");
        if (judge === true) {
            let reason = prompt("申请理由");
            if (reason !== null && reason !== "") {
                //$('#reason').val(reason);
                document.getElementById('reason').value = reason;
                $.get({
                    url: "/student/ajax/applyUpdateStuDorm",
                    data: {"sno": sno, "sign": 2, "reason": reason},
                    async: false, method: 'get',
                    // dataType: "text",
                    success: function (result) {
                        //alert(result)
                        if (result === true) {
                            $('#form-update-dorm').submit(function () {
                                alert("申请成功");
                            })
                        }
                    },
                    error: function (result2) {
                        console.log("=====" + result2)
                        alert("error");
                    }
                })

            }
        } else {

        }
    })
    $('#btn-apply').click(function () {

        let judge = confirm("确定申请住校?");
        if (judge === true) {
            $.get({
                url: "/student/ajax/getStuDorm",
                data: {"sno": sno},
                success: function (result) {
                    if (result === true) {
                        $.get({
                            url: "/student/ajax/addStuDorm",
                            data: {"sno": sno, "sname": sname, "sex": sex},
                            success: function (value) {
                                if (value === true) {
                                    alert("申请成功!");
                                } else {
                                    alert("申请失败!请找管理人员");
                                }
                            }
                        })
                    } else {
                        alert("你已经申请过了!请勿重复申请!");
                    }
                }
            })
        } else {

        }

    })
    $("#btn-quitDorm-apply").click(function () {
        $.get({
            url: "/student/ajax/judgeStuDorm",
            data: {"sno": sno},
            success: function (result) {
                if (result === null) {
                    alert("你未申请住校!无法撤销");
                } else {
                    if (result.dorm_id !== null) {
                        let result = confirm("是否撤销你的住校申请!");
                        if (result === true) {
                            alert("你无法撤销!")
                        } else {

                        }
                    } else {
                        let result = confirm("是否撤销你的住校申请!");
                        if (result === true) {
                            $.get({
                                url: "/student/ajax/quitDormApply",
                                data: {"sno": sno},
                                success: function (result) {
                                    if (result === true) {
                                        alert("你已撤销!");
                                    } else {
                                        alert("撤销失败!没有你的申请记录!");
                                    }
                                }
                            })
                        } else {

                        }
                    }
                }
            }
        })
    })
    $('#bed_id').change(function () {
        let bedId = $('#bed_id');
        let dormId = $('#dorm_id');
        if (bedId.val() !== null && bedId.val() !== "") {
            $.get({
                url: "/admin/ajax/getDorm",
                data: {"dorm_id": dormId.val()},
                success: function (dorm) {
                    if (dorm !== null) {
                        let number = dorm.dorm_number;
                        if (bedId.val() > number || bedId.val() < 1) {
                            alert("你输入的床位不合理!请重新输入");
                            /* let warn = $('#warn-bed-num');
                             warn.text("你输入的床位不合理!请重新输入");
                             warn.css("color","red");
                             warn.show();*/
                            bedId.val("");
                        } else {
                            $.get({
                                url: "/admin/ajax/getStudentDorm",
                                data: {"dorm_id": dormId.val(), "bed_id": bedId.val()},
                                success: function (result) {
                                    if (result === false) {
                                        alert("该寝室床位已经有人登记注册了!请选择其他寝室床位!");
                                        /*  let warn = $('#warn-bed-num');
                                          warn.text("该寝室床位已经有人登记注册了!请选择其他寝室床位!");
                                          warn.css("color","red");
                                          warn.show();*/
                                        bedId.val("");
                                    }
                                }
                            })
                        }
                    }
                }
            })
        }
    })
    $('#dorm_id').change(function () {
        let bedId = $('#bed_id');
        let dormId = $('#dorm_id');
        $.get({
            url: "/admin/ajax/judgeStuDormNumber",
            data: {"dorm_id": dormId.val()},
            success: function (result) {
                if (result === false) {
                    alert("该寝室人员已满!请换另一个寝室!");
                    /*let warn = $('#warn-dorm-num');
                    warn.text("该寝室人员已满!请换另一个寝室!");
                    warn.css("color","red");
                    warn.show();*/
                }
            }
        })
    })
})